var s_b_f_isSelected = false;
var s_b_f_all_amount = 0;
var s_b_f_sending = new Array();
var s_b_f_senddone_amount = 0;
var s_b_f_allow = false;
var s_b_f_fileobj = null;
var s_b_f_uid = 0;
var s_b_f_name = "";
var s_b_f_divid = "";
var s_b_f_initmessage = "";
var s_b_f_initcode = -1000;
var s_b_f_accept = "*";
function s_b_f_sendCell(index, start, end) {
	var form = new FormData();
	form.append("data", s_b_f_fileobj.slice(start, end));
	form.append("uid", s_b_f_uid);
	form.append("index", index + 1);
	form.append("orgianname", s_b_f_name);
	$
			.ajax({
				url : "index.php?mod=home&childmod=home&ajax=true&op=general_big_file_update_cell",
				type : "POST",
				data : form,
				dataType : 'json',
				async : true,
				processData : false,
				contentType : false,
				success : s_b_f_onSuccess,
				error : s_b_f_onError
			});
}

function s_b_f_startupdate() {
	$("#s_b_reset").addClass("disabled");
	$("#s_b_update").addClass("disabled");
	$(".s_b_f_progress").show();
	if (!s_b_f_isSelected || !s_b_f_allow)
		return;
	var shardSize = 2 * 1024 * 1024;
	var size = s_b_f_fileobj.size;
	s_b_f_all_amount = Math.ceil(size / shardSize);
	s_b_f_update_header(s_b_f_name, s_b_f_all_amount, size);

}
function s_b_f_onSuccess(data) {
	s_b_f_operatedone(data.code, data.message, data.data);
}

function s_b_f_onError(data) {
	console.log(data);
	s_b_f_operatedone(-101, "服务器返回了未知的数据，请上报管理员", data.responseText);
}
function s_b_f_showMessage(message) {
	$(".s_b_f_desc").show();
	$(".s_b_f_desc").html(message);
}
function s_b_f_operatedone(id, message, data) {
	if (id == -101) {
		s_b_f_showMessage(message + "<br><br><pre>" + data + "</pre>");
		return;
	}

	if (data.op == "allow") {
		s_b_f_showMessage("您已经选择" + s_b_f_name + "已经得到上传授权，如需上传请按确定");
		$(".s_f_desc").hide();
		s_b_f_allow = true;
		$("#s_b_update").removeClass("disabled");
		return;
	}
	if (data.op == "reject") {
		s_b_f_showMessage(message);
		s_b_f_reset();
		s_b_f_init();
	}
	if (data.op == "header_callback") {
		s_b_f_uid = data.uid;
		start_update();
	}
	if (data.op == "done") {
		if (typeof (eval("s_b_f_done_callback"))) {
			s_b_f_done_callback(data.data);
		}
		s_b_f_showMessage("文件：" + s_b_f_name + "已上传成功");
		s_b_f_reset();
		s_b_f_init();
	}
	if (data.op == "updating") {
		$('.s_b_f_progress').progress({
			percent : parseInt((data.now / s_b_f_all_amount) * 100),
			text : {
				active : '上传文件中,若卡99%请等待一会，服务器正在进行文件合并'
			}
		});
	}
}
function s_b_f_setInitMessage(code, message) {
	s_b_f_initcode = code;
	s_b_f_initmessage = message;
}
function s_b_f_resetInitMessage() {
	$("#s_b_f_message").addClass("hidden");
	s_b_f_initcode = -1000;
	s_b_f_initmessage = "";
}
function s_b_f_showInitMessage() {
	if (s_b_f_initcode == -1000) {
		$("#s_b_f_message").addClass("hidden");
		return false;
	}
	$("#s_b_f_message").removeClass("hidden");
	console.log(s_b_f_initmessage);
	showMessage("s_b_f_message", s_b_f_initcode, s_b_f_initmessage);
}
function start_update() {
	var shardSize = 2 * 1024 * 1024;
	var size = s_b_f_fileobj.size;
	s_b_f_all_amount = Math.ceil(size / shardSize);
	for (var i = 0; i < s_b_f_all_amount; ++i) {
		$('.s_b_f_progress').progress({
			percent : parseInt((i / s_b_f_all_amount) * 100),
			total : s_b_f_all_amount,
			text : {
				active : '裁剪文件中'
			}
		});
		var start = i * shardSize, end = Math.min(size, start + shardSize);
		s_b_f_sendCell(i, start, end);
	}
}
function s_b_f_reset() {
	s_b_f_isSelected = false;
	s_b_f_all_amount = 0;
	s_b_f_sending = new Array();
	s_b_f_senddone_amount = 0;
	s_b_f_allow = false;
	s_b_f_fileobj = null;
	s_b_f_uid = 0;
	s_b_f_name = "";
	s_b_f_divid = "";
}

function s_b_f_init() {
	$(".s_b_f_progress").hide();
	$("#s_b_choose").removeClass("disabled");
	$("#s_b_reset").addClass("disabled");
	$("#s_b_update").addClass("disabled");
	$(".s_f_desc").show();
	$("#s_f_input_updater").attr("accept", s_b_f_accept);
	s_b_f_showInitMessage();

}

function s_b_f_ask_allow(size) {
	ajaxRequest(
			"size=" + size,
			"index.php?mod=home&childmod=home&ajax=true&op=general_big_file_update_checkSize",
			s_b_f_onSuccess, s_b_f_onError);
}
function s_b_f_update_header(name, all, size) {
	ajaxRequest(
			"size=" + size + "&name=" + name + "&allamount=" + all,
			"index.php?mod=home&childmod=home&ajax=true&op=general_big_file_update_header",
			s_b_f_onSuccess, s_b_f_onError);

}
function s_b_f_onFileUpdaterChenge(e) {
	s_b_f_fileobj = e[0].files[0];
	if (s_b_f_fileobj == null || s_b_f_fileobj == undefined) {
		s_b_f_showMessage("没有选择文件，请重新选择");
		return;
	}
	s_b_f_resetInitMessage();
	s_b_f_name = s_b_f_fileobj.name;
	s_b_f_ask_allow(s_b_f_fileobj.size);
	s_b_f_isSelected = true;
	$("#s_b_choose").addClass("disabled");
	$("#s_b_reset").removeClass("disabled");
}
function s_b_f_rechoose() {
	s_b_f_refresh();
	s_b_f_showMessage("文件缓冲区已重置，请重新选择您的文件");
}
function s_b_f_refresh() {
	s_b_f_load(s_b_f_divid);
}
function s_b_f_load(divid) {
	$("#" + s_b_f_divid).html("");
	s_b_f_reset();
	s_b_f_divid = divid;
	$("#" + s_b_f_divid)
			.load(
					"index.php?mod=home&childmod=home&ajax=true&op=file_updater_insert");
}

function s_b_f_uninstall() {
	$("#" + s_b_f_divid).html("");
	s_b_f_reset();
}

function s_b_f_setAccept(accept) {
	s_b_f_accept = accept;
}